﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Threading;
using HtmlAgilityPack;

namespace WindowsFormsApp1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            Control.CheckForIllegalCrossThreadCalls = false;
        }
        //显示网页
        void ThreadBrowser1() {
            webBrowser1.ScriptErrorsSuppressed = true;
            webBrowser1.Refresh();
            webBrowser1.Url = new Uri("https://www.baidu.com/s?wd=" + textBox1.Text);
            webBrowser1.Navigate("https://www.baidu.com/s?wd=" + textBox1.Text);
        }
        void ThreadBrowser2() {
            webBrowser2.ScriptErrorsSuppressed = true;
            webBrowser2.Refresh();
            webBrowser2.Url = new Uri("https://www.bing.com/search?q=" + textBox1.Text);
            webBrowser2.Navigate("https://www.bing.com/search?q=" + textBox1.Text);
        }
        //显示文本
        void ThreadBaidu() {
            lock(this){
                HtmlWeb web = new HtmlWeb();
                //从url中加载
                HtmlAgilityPack.HtmlDocument doc = web.Load("https://www.baidu.com/s?wd=" + textBox1.Text);
                HtmlNodeCollection nodes = doc.DocumentNode.SelectNodes("//div[@class='result c-container xpath-log new-pmd']");

                string html = "";
                foreach (var node in nodes) {
                    html += node.InnerText;
                }
                int index = 0;//字符个数
                foreach (var i in html) {
                    if (index < 200 && i > 32 && i != ' ' && i != '\n' && i != '\r' && i != '\t') {
                        richTextBox1.Text += i;
                        index++;
                    }
                }
            }
        }
        void ThreadBing() {
            lock (this) {
                HtmlWeb web = new HtmlWeb();
                //从url中加载
                HtmlAgilityPack.HtmlDocument doc = web.Load("https://cn.bing.com/search?q=" + textBox1.Text);
                HtmlNode Node = doc.DocumentNode.SelectSingleNode("//*[@id='b_results']");

                //获得所有子节点
                HtmlNodeCollection nodes = Node.ChildNodes;
                string html = "";
                foreach (var node in nodes) {
                    html += node.InnerText;
                }
                int index = 0;//字符个数
                foreach (var i in html) {
                    if (index < 200 && i > 32 && i != ' ' && i != '\n' && i != '\r' && i != '\t') {
                        richTextBox2.Text += i;
                        index++;
                    }
                }
            }
        }
        private void Button1_Click(object sender, EventArgs e)
        {
            
            try
            {
                new Thread(ThreadBrowser2).Start();
                new Thread(ThreadBrowser1).Start();
                
            }
            catch (Exception exception)
            {
                textBox1.Text = "出错！";
            }
        }
        
        private void Button2_Click(object sender,EventArgs e)
        {
            richTextBox1.Text = "百度搜索";
            richTextBox2.Text = "必应搜索";
            try {
                new Thread(ThreadBaidu).Start();
                new Thread(ThreadBing).Start();
                
            }
            catch(Exception exception) {
                textBox1.Text = "出错！";
            }

        }
        

        private void textBox1_TextChanged(object sender, EventArgs e) {

        }

        private void Form1_Load(object sender, EventArgs e) {

        }
    }
}
